package test;

/**
 * @author WYC
 * @version 1.0
 * @ClassName FactoryExample
 * @date 2024/12/14 0:46
 * @description
 */
// 产品接口
interface Animal {
    void sound();
}

// 具体产品 - Dog
class Dog implements Animal {
    public void sound() {
        System.out.println("汪汪！");
    }
}

// 具体产品 - Cat
class Cat implements Animal {
    public void sound() {
        System.out.println("喵喵！");
    }
}

// 工厂接口
interface AnimalFactory {
    Animal createAnimal();
}

// 具体工厂 - DogFactory
class DogFactory implements AnimalFactory {
    public Animal createAnimal() {
        return new Dog();
    }
}

// 具体工厂 - CatFactory
class CatFactory implements AnimalFactory {
    public Animal createAnimal() {
        return new Cat();
    }
}

public class FactoryExample {
    public static void main(String[] args) {
        // 使用工厂模式创建产品
        AnimalFactory dogFactory = new DogFactory();
        Animal dog = dogFactory.createAnimal();
        dog.sound();

        AnimalFactory catFactory = new CatFactory();
        Animal cat = catFactory.createAnimal();
        cat.sound();
    }
}
